package com.hm.service.api;

import jakarta.servlet.http.HttpServletRequest;
import org.opencv.core.*;
import org.opencv.imgcodecs.Imgcodecs;
import org.opencv.imgproc.Imgproc;
import org.opencv.objdetect.CascadeClassifier;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;

/**
 * 参考链接
 * https://www.cnblogs.com/yszzu/p/10257039.html
 */

@RestController
@RequestMapping(value = "/MAIN", produces = {"application/json;charset=UTF-8"})
public class TestOpenCV1 {

    private static final Logger logger = LoggerFactory.getLogger(TestOpenCV1.class);


    //初始化人脸探测器
    static CascadeClassifier faceDetector;

    //测试openCV
    public TestOpenCV1() {

    }

    @GetMapping(value = "/test1")
    public String doPost(HttpServletRequest request) {
        logger.error("-----------请求进来了-----------");
        // 加载Haar特征的人脸检测模型
        faceDetector = new CascadeClassifier("D:/opencv-test/haarcascade_frontalface_alt.xml");

        // 读取图像
        String imagePath1 = "D:/opencv-test/face1.png";
        String imagePath2 = "D:/opencv-test/face2.png";

        //图片预处理
        Mat img1 = Imgcodecs.imread(imagePath1, Imgcodecs.IMREAD_GRAYSCALE);
        Mat img2 = Imgcodecs.imread(imagePath2, Imgcodecs.IMREAD_GRAYSCALE);

        MatOfRect faces1 = new MatOfRect();
        MatOfRect faces2 = new MatOfRect();

        faceDetector.detectMultiScale(img1, faces1);
        faceDetector.detectMultiScale(img2, faces2);

        int size1 = faces1.toList().size();
        int size2 = faces2.toList().size();

        logger.error("-size1->" + size1);
        logger.error("-size2->" + size2);

        if (size1 > 1 || size2 > 1) {
            return "图片存在两张以上脸";
        }

        if (size1 == 0 || size2 == 0) {
            return "图片未检测到人脸";
        }
        //大于0.72属于人脸匹配成功


        double compareHist = compare_image(imagePath1, imagePath2);

        return "请求成功--->" + compareHist;
    }

    public static double compare_image(String img_1, String img_2) {
        Mat mat_1 = conv_Mat(img_1);
        Mat mat_2 = conv_Mat(img_2);

        Mat hist_1 = new Mat();
        Mat hist_2 = new Mat();

        //颜色范围
        MatOfFloat ranges = new MatOfFloat(0f, 256f);
        //直方图大小， 越大匹配越精确 (越慢)
        MatOfInt histSize = new MatOfInt(1000);

        Imgproc.calcHist(Arrays.asList(mat_1), new MatOfInt(0), new Mat(), hist_1, histSize, ranges);
        Imgproc.calcHist(Arrays.asList(mat_2), new MatOfInt(0), new Mat(), hist_2, histSize, ranges);

        // CORREL 相关系数
        double res = Imgproc.compareHist(hist_1, hist_2, Imgproc.CV_COMP_CORREL);
        return res;
    }

    private static Mat conv_Mat(String img_1) {
        Mat image0 = Imgcodecs.imread(img_1);

        Mat image = new Mat();
        //灰度转换
        Imgproc.cvtColor(image0, image, Imgproc.COLOR_BGR2GRAY);

        MatOfRect faceDetections = new MatOfRect();
        //探测人脸
        faceDetector.detectMultiScale(image, faceDetections);

        // rect中是人脸图片的范围
        for (Rect rect : faceDetections.toArray()) {
            //切割rect人脸
            Mat mat = new Mat(image, rect);
            return mat;
        }
        return null;
    }


}
